package com.dalei.vds;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dalei.vds.settings.ServerSettings;
import com.dalei.vds.telephone.net.WifiConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ServerSettingsActivity extends Activity {
	
	public static final int REQUEST_SVR_ARGS_SETTINGS = 0x00000010;
	
	private static final int MESSAGE_SHOW_DIALOG = 0x00000000;

	private static final int MESSAGE_DISMISS_DIALOG = 0x00000001;

	private static final int MESSAGE_SHOW_TOAST = 0x00000002;
	
	private static final int MESSAGE_UPDATE_WIFI_STATUS = 0x00000003;

	private static final int MESSAGE_UPDATE_SERVER_STATUS = 0x00000004;
	
	private static final String WIFI_STATUS = "wifiStatus";
	
	private static final String LOCAL_IPADDRESS = "localIpAddress";
	
	private static final String SSID = "ssid";
	
	private ProgressDialog dialog;
	
	private TextView wifiStatusText;

	private TextView ssidText;

	private TextView localIpAddressText;

	private ListView menuListView;

	private TextView hostNameText;

	private TextView ipAddressText;

	private TextView portText;

	private TextView timeoutText;

	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.serversettings);

		handler = new IncomingHandler(this);

		menuListView = (ListView) findViewById(R.id.serversettings_menu);
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		Map<String, String> menuItem = new HashMap<String, String>();
		menuItem.put("title", getString(R.string.con_args_settings));
		menuItem.put("description",
				getString(R.string.con_args_settings_description));
		data.add(menuItem);
		menuListView.setAdapter(new SimpleAdapter(this, data,
				R.layout.list_menuitem,
				new String[] { "title", "description" }, new int[] {
						R.id.list_menuitem_title,
						R.id.list_menuitem_description }));
		menuListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					Intent intent = new Intent();
					intent.setClass(ServerSettingsActivity.this,
							SvrArgsSettingsActivity.class);
					ServerSettingsActivity.this.startActivityForResult(intent,
							REQUEST_SVR_ARGS_SETTINGS);
					break;

				default:
					break;
				}
			}
		});
		
		wifiStatusText = (TextView) findViewById(R.id.serversettings_wifistatus);

		ssidText = (TextView) findViewById(R.id.serversettings_ssid);

		localIpAddressText = (TextView) findViewById(R.id.serversettings_local_ipaddress);

		hostNameText = (TextView) findViewById(R.id.serversettings_hostname);

		ipAddressText = (TextView) findViewById(R.id.serversettings_ipaddress);

		portText = (TextView) findViewById(R.id.serversettings_port);

		timeoutText = (TextView) findViewById(R.id.serversettings_timeout);

		Thread thread = new OnLoadThread(this);
		thread.start();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUEST_SVR_ARGS_SETTINGS:
			switch (resultCode) {
			case SvrArgsSettingsActivity.RESULT_ARGS_CHANGED:
				Thread thread = new OnLoadThread(this);
				thread.start();
				break;

			default:
				break;
			}
			break;

		default:
			break;
		}
	}

	private static class IncomingHandler extends Handler {

		private WeakReference<ServerSettingsActivity> reference;

		public IncomingHandler(ServerSettingsActivity activity) {
			// TODO Auto-generated constructor stub
			reference = new WeakReference<ServerSettingsActivity>(activity);
		}

		@Override
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			ServerSettingsActivity activity = reference.get();
			switch (msg.what) {
			case MESSAGE_SHOW_DIALOG:
				activity.dialog = ProgressDialog.show(activity, null,
						activity.getString(R.string.wait_loading_data));
				break;

			case MESSAGE_DISMISS_DIALOG:
				activity.dialog.dismiss();
				break;

			case MESSAGE_SHOW_TOAST:
				Toast.makeText(activity, msg.arg1, Toast.LENGTH_SHORT).show();
				break;
				
			case MESSAGE_UPDATE_WIFI_STATUS:
				Map<String, String> wifiStatusData = (Map<String, String>) msg.obj;
				activity.wifiStatusText.setText(wifiStatusData.get(WIFI_STATUS));
				activity.ssidText.setText(wifiStatusData.get(SSID));
				activity.localIpAddressText.setText(wifiStatusData.get(LOCAL_IPADDRESS));
				break;

			case MESSAGE_UPDATE_SERVER_STATUS:
				Map<String, String> serverStatusData = (Map<String, String>) msg.obj;
				activity.hostNameText
						.setText(serverStatusData.get(ServerSettings.HOSTNAME));
				activity.ipAddressText.setText(serverStatusData
						.get(ServerSettings.IPADDRESS));
				activity.portText.setText(serverStatusData.get(ServerSettings.PORT));
				activity.timeoutText.setText(serverStatusData.get(ServerSettings.TIMEOUT));
				break;

			default:
				break;
			}
		}

	}

	private class OnLoadThread extends Thread {
		
		private ServerSettings settings;
		
		public OnLoadThread(Context context) {
			// TODO Auto-generated constructor stub
			settings = new ServerSettings(context);
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = null;
			handler.sendEmptyMessage(ServerSettingsActivity.MESSAGE_SHOW_DIALOG);
			switch (settings.debug()) {
			
			case ServerSettings.NETWORK_NOT_AVAILABLE:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.network_not_connected;
				handler.sendMessage(msg);
				break;
				
			case ServerSettings.SETTINGS_NOT_FOUND:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.setting_not_found;
				handler.sendMessage(msg);
				break;
				
			case ServerSettings.INVALID_IP_ADDRESS:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.invalid_ip_address;
				handler.sendMessage(msg);
				break;

			case ServerSettings.IP_ADDRESS_NOT_REACHABLE:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.ip_address_not_reachadle;
				handler.sendMessage(msg);
				break;

			case ServerSettings.PORT_NOT_OPENED:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.port_not_opened;
				handler.sendMessage(msg);
				break;
				
			case ServerSettings.INTERNAL_ERROR:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.internal_error;
				handler.sendMessage(msg);
				break;

			case ServerSettings.WEB_SERVICE_NOT_AVAILABLE:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_SHOW_TOAST;
				msg.arg1 = R.string.webservice_not_available;
				handler.sendMessage(msg);
				break;

			case ServerSettings.SUCCESS:
				handler.sendEmptyMessage(MESSAGE_DISMISS_DIALOG);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_WIFI_STATUS;
				msg.obj = getWifiStatusData();
				handler.sendMessage(msg);
				msg = handler.obtainMessage();
				msg.what = MESSAGE_UPDATE_SERVER_STATUS;
				msg.obj = getServerStatusData();
				handler.sendMessage(msg);
				break;

			default:
				break;
			}
		}
		
		private Map<String, String> getWifiStatusData() {
			Map<String, String> wifiStatusData = new HashMap<String, String>();
			WifiConnection connection = new WifiConnection(ServerSettingsActivity.this);
			if (connection.isConnected()) {
				wifiStatusData.put(WIFI_STATUS, getString(R.string.connected));
			} else {
				wifiStatusData.put(WIFI_STATUS, getString(R.string.not_connected));
			}
			String ssid = connection.getSSID();
			if (ssid != null) {
				wifiStatusData.put(SSID, ssid);
			} else {
				wifiStatusData.put(SSID, getString(R.string.not_available));
			}
			String localIpAddress = connection.getIpAddress();
			if (localIpAddress != null) {
				wifiStatusData.put(LOCAL_IPADDRESS, localIpAddress);
			} else {
				wifiStatusData.put(LOCAL_IPADDRESS, getString(R.string.not_available));
			}
			return wifiStatusData;
		}
		
		private Map<String, String> getServerStatusData() {
			Map<String, String> serverStatusData = new HashMap<String, String>();
			String hostName = settings.getHostName();
			if (hostName != null) {
				serverStatusData.put(ServerSettings.HOSTNAME, hostName);
			} else {
				serverStatusData.put(ServerSettings.HOSTNAME,
						getString(R.string.not_available));
			}
			String ipAddress = settings.getIpAddress();
			if (ipAddress != null) {
				serverStatusData.put(ServerSettings.IPADDRESS, ipAddress);
			} else {
				serverStatusData.put(ServerSettings.IPADDRESS,
						getString(R.string.not_available));
			}
			int port = settings.getPort();
			if (port >= 0) {
				serverStatusData.put(ServerSettings.PORT, Integer.toString(port));
			} else {
				serverStatusData.put(ServerSettings.PORT,
						getString(R.string.not_available));
			}
			int timeout = settings.getTimeout();
			if (timeout >= 0) {
				serverStatusData.put(ServerSettings.TIMEOUT, Integer.toString(timeout));
			} else {
				serverStatusData.put(ServerSettings.TIMEOUT,
						getString(R.string.not_available));
			}
			return serverStatusData;
		}
	}
}